package com.sky.controller.admin;

import com.sky.dto.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "订单管理相關接口")
@RestController("adminOrderController")
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/conditionSearch")
    public Result<PageResult> searchByCondition(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("訂單條件搜索：{}", ordersPageQueryDTO);
        PageResult pageResult = orderService.searchByCondition(ordersPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 統計各個狀態的訂單的數量
     * @return
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> getStatistics(){
        log.info("統計各個狀態的訂單的數量：");
        OrderStatisticsVO orderStatisticsVO = orderService.getByStatistics();

        return Result.success(orderStatisticsVO);
    }


    /**
     * 查詢訂單詳情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    public Result<OrderVO> seeDetails(@PathVariable Long id){
        log.info("管理端查看訂單詳情：{}", id);
        OrderVO orderVO = orderService.getDetails(id);
        return Result.success(orderVO);
    }


    /**
     * 接受訂單
     * @param ordersConfirmDTO
     * @return
     */
    @PutMapping("/confirm")
    public Result Ordering(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("商家接單：{}", ordersConfirmDTO);
        orderService.Ordering(ordersConfirmDTO);
        return Result.success();
    }


    /**
     * 商家拒單
     * @param ordersRejectionDTO
     */
    @PutMapping("/rejection")
    public Result Declines(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("商家拒單：{}", ordersRejectionDTO);
        orderService.Declines(ordersRejectionDTO);
        return Result.success();
    }


    /**
     * 商家取消訂單
     * @param ordersCancelDTO
     */
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("商家取消訂單：{}", ordersCancelDTO);
        orderService.cancel(ordersCancelDTO);
        return Result.success();
    }


    /**
     * 商家派送訂單
     * @param id
     */
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable Long id) {
        log.info("訂單派送：{}", id);
        orderService.delivery(id);
        return Result.success();
    }


    /**
     * 商家完成訂單
     * @param id
     */
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id) {
        log.info("完成訂單：{}", id);
        orderService.complete(id);

        return Result.success();
    }
}
